import { contextBridge, ipcRenderer } from "electron";

interface PrimaryWindowApi {
  sendMessage: (message: string) => void;
  showFramelessSampleWindow: () => void;
  openExternalLink: (url: string) => void;
  clearAppConfiguration: () => void;
  onShowExitAppMsgbox: (callback: () => void) => void;
  onShowClosePrimaryWinMsgbox: (callback: () => void) => void;
  asyncExitApp: () => Promise<void>;
  minToTray: () => void;
  httpGetRequest: (url: string) => void;
  runAutomation: () => Promise<{ success: boolean; message: string }>;
  readExcelFile: (filePath: string) => Promise<any>;
  submitTrafficData: (data: any) => Promise<any>;
  openDevTools: () => Promise<boolean>;
  getCookie: (options: any) => Promise<string | null>;
  updateCookie: (
    cookieString: string
  ) => Promise<{ success: boolean; message: string }>;
  createWorkOrder: (data: any) => Promise<any>;
  autoLoginGetCookie: (options: {
    loginUrl: string;
    username: string;
    password: string;
  }) => Promise<{ success: boolean; cookie?: string; message?: string }>;
  setAutoSubmit: (scheduledTime: string) => Promise<void>;
  cancelAutoSubmit: () => Promise<void>;
  uploadImage: () => Promise<{
    success: boolean;
    attachCode?: string;
    message?: string;
  }>;
}

// 暴露通用的electron API
contextBridge.exposeInMainWorld("electron", {
  openDevTools: () => ipcRenderer.send("open-dev-tools"),
  ipcRenderer: {
    invoke: (channel: string, ...args: any[]) =>
      ipcRenderer.invoke(channel, ...args),
    on: (channel: string, listener: (event: any, ...args: any[]) => void) => {
      ipcRenderer.on(channel, listener);
    },
  },
});

/*
暴露primary窗口主进程的方法到primary窗口的渲染进程
*/
contextBridge.exposeInMainWorld("primaryWindowAPI", {
  sendMessage: (message: string) => ipcRenderer.send("send-message", message),
  showFramelessSampleWindow: () =>
    ipcRenderer.send("show-frameless-sample-window"),
  openExternalLink: (url: string) =>
    ipcRenderer.send("open-external-link", url),
  clearAppConfiguration: () => ipcRenderer.send("clear-app-configuration"),
  onShowExitAppMsgbox: (callback: () => void) =>
    ipcRenderer.on("show-exit-app-msgbox", () => callback()),
  onShowClosePrimaryWinMsgbox: (callback: () => void) =>
    ipcRenderer.on("show-close-primary-win-msgbox", () => callback()),
  asyncExitApp: () => ipcRenderer.invoke("async-exit-app"),
  minToTray: () => ipcRenderer.send("min-to-tray"),
  httpGetRequest: (url: string) => ipcRenderer.send("http-get-request", url),
  runAutomation: () => ipcRenderer.invoke("run-automation"),
  readExcelFile: (filePath: string) =>
    ipcRenderer.invoke("read-excel-file", filePath),
  submitTrafficData: (data: any) =>
    ipcRenderer.invoke("submit-traffic-data", data),
  openDevTools: () => ipcRenderer.invoke("open-devtools"),
  getCookie: (options: any) => ipcRenderer.invoke("get-cookie", options),
  updateCookie: (cookieString: string) =>
    ipcRenderer.invoke("update-cookie", cookieString),
  createWorkOrder: (data: any) => ipcRenderer.invoke("create-work-order", data),
  autoLoginGetCookie: (options: any) =>
    ipcRenderer.invoke("auto-login-get-cookie", options),
  setAutoSubmit: (scheduledTime: string) =>
    ipcRenderer.invoke("set-auto-submit", scheduledTime),
  cancelAutoSubmit: () => ipcRenderer.invoke("cancel-auto-submit"),
  uploadImage: () => ipcRenderer.invoke("upload-image"),
} as PrimaryWindowApi);
